package com.tj.jinzao.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.tj.jinzao.R;
import com.tj.jinzao.activity.ArticleDetailActivity;
import com.tj.jinzao.activity.PersonArticleActivity;
import com.tj.jinzao.adapter.ArticleListItemAdapter;
import com.tj.jinzao.adapter.HotListItemAdapter;
import com.tj.jinzao.base.BaseDataListFragment;
import com.tj.jinzao.callback.IListView;
import com.tj.jinzao.model.Article;
import com.tj.jinzao.presenter.ArticlePresenter;
import com.tj.jinzao.utils.Utils;
import com.tj.jinzao.widgets.EmptyView;

import java.util.HashMap;
import java.util.List;

/**
 * 社区文章列表
 * Created by wjy on 2019/1/1.
 */
public class ArticleListFragment extends BaseDataListFragment<List<Article>, ArticlePresenter, Article> implements IListView<List<Article>> {

    public final static int TYPE_FOLLOW = 0;
    public final static int TYPE_RECOMMEND = 1;
    public final static int TYPE_HOT = 2;
    public final static int TYPE_VIDEO = 3;
    public final static int TYPE_MY_COLLECT_DAILY = 5;
    public final static int TYPE_MY_COLLECT_VIDEO = 6;
    public final static int TYPE_MY_COLLECT_TOPIC = 7;

    public static ArticleListFragment newInstance(int type) {
        ArticleListFragment fragment = new ArticleListFragment();
        if (fragment != null) {
            Bundle args = new Bundle(3);
            args.putString(ARG_TAG_ID, String.valueOf(type));
            args.putInt(ARG_LOAD_ID, 1);
            args.putInt(ARG_LAYOUT_RES_ID, R.layout.base_fragment_item_layout);
            fragment.setArguments(args);
        }
        return fragment;
    }

    @Override
    public void onRequestNoData(String message) {
        setEmptyDataView();
    }

    @Override
    protected ArticlePresenter createPresenter() {
        return new ArticlePresenter(this);
    }

    @Override
    protected void createView(LayoutInflater inflater, View view) {
    }

    @Override
    protected RecyclerView.Adapter createAdapter() {
        if (Utils.parseInt(mTag) == TYPE_HOT) {
            return new HotListItemAdapter(mContext, mData);
        }
        ArticleListItemAdapter adapter = new ArticleListItemAdapter(mContext, mData);
        adapter.setListener(this);
        return adapter;
    }


    @Override
    protected void requestData() {
        HashMap<String, Object> map = new HashMap();
        map.put("currentPage", mPageNo);
        switch (Utils.parseInt(mTag)) {
            case TYPE_FOLLOW:
                map.put("collectionType", 3);
                mPresenter.getMyCollectArticleList(map);
                break;
            case TYPE_HOT:
                map.put("hot", true);
                map.put("type", 2);
                mPresenter.getArticleList(map, true);
                break;
            case TYPE_RECOMMEND:
                map.put("recommend", true);
                mPresenter.getArticleList(map, true);
                break;
            case TYPE_VIDEO:
                map.put("video", true);
                mPresenter.getArticleList(map, true);
                break;
            case TYPE_MY_COLLECT_DAILY:
                map.put("collectionType", 2);
                map.put("type", 1);
                mPresenter.getMyCollectArticleList(map);
                break;
            case TYPE_MY_COLLECT_VIDEO:
                map.put("collectionType", 2);
                map.put("video", true);
                mPresenter.getMyCollectArticleList(map);
                break;
            case TYPE_MY_COLLECT_TOPIC:
                map.put("collectionType", 2);
                map.put("type", 2);
                mPresenter.getMyCollectArticleList(map);
                break;
        }
    }

    @Override
    protected void setEmptyDataView() {
        mEmptyView.setVisibility(View.VISIBLE);
        mEmptyView.setState(EmptyView.State.NO_DATA, "没有内容哦");
    }

    @Override
    public void onRequestSuccess(List<Article> articles) {
        super.onRequestSuccess(articles);
        if(mData!=null){
            for (int i=0;i<mData.size();i++){
                if(mData.get(i).getInvid()==1){
                    mData.clear();
                }
            }
        }
        if (mPageNo <= 1) {
            mData.clear();
        }
        mData.addAll(articles);
        for (int i=0;i<mData.size();i++){
            mData.get(i).setInvid(0);
        }
        showView();
    }
    protected void showView() {
        TextView txt= view.findViewById(R.id.empty_text);
        if (mData.size() != 0) {
            mListView.setVisibility(View.VISIBLE);
            mEmptyView.setVisibility(View.GONE);
            txt.setVisibility(View.GONE);
        } else {
            mListView.setVisibility(View.VISIBLE);
            mEmptyView.setVisibility(View.GONE);
            txt.setVisibility(View.VISIBLE);
//            setEmptyDataView();
        }
        //不要问为什么，这是一种骚操作- - (项目进度赶，之后再改)
        if(mData.size()==0){
            Article article=new Article();
            article.setInvid(1);
            mData.add(article);
        }
        mListView.getAdapter().notifyDataSetChanged();
    }

    @Override
    protected boolean resolveData(List<Article> characters) {
        return false;
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void itemClicked(int position, Article article) {
        if(article!=null&&article.getInvid()!=1){
            startActivity(new Intent(mContext, ArticleDetailActivity.class).putExtra("article", article));
        }

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        int position = (Integer) v.getTag(R.id.position);
        Article article = mData.get(position);
        switch (v.getId()) {
            case R.id.iv_head:
                startActivity(new Intent(mContext, PersonArticleActivity.class)
                        .putExtra("user", article.getUser()));
                break;
            case R.id.tv_follow:
                mPresenter.followUser(mData.get(position).getUser().getId());
                break;
        }
    }

    @Override
    public void onRequestSuccessPage(int currentPage, int allPage, int allSize) {
        mPageNum = allPage;
    }

    @Override
    public void onReload(Object tag) {
        if (tag != null) {
            if (tag instanceof Long) {
                int position = ((Long) tag).intValue();
                boolean hasFollow = mData.get(position).isHasFollow();
                mData.get(position).setHasFollow(!hasFollow);
                mListView.getAdapter().notifyDataSetChanged();
            }
        }

    }
}
